home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Xconq 7.1.0 / src / xconq-7.1.0 / kernel / world.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-07  |  17.3 KB  |  498 lines  |  [TEXT/R*ch]

  1. /* Definitions relating to worlds and areas in Xconq.
  2.    Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995
  3.    Stanley T. Shebs.
  4.  
  5. Xconq is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.  See the file COPYING.  */
  9.  
  10. typedef struct a_world {
  11.     int circumference;        /* number of cells going around the world */
  12.     int daylength;        /* number of turns in a day */
  13.     int yearlength;        /* number of turns in a year */
  14.     int axialtilt;        /* controls extrema of seasons */
  15.     int daylight_width;        /* width of area that is lit */
  16.     int twilight_width;        /* width of area that is at least partly lit */
  17. } World;
  18.  
  19. /* Theoretically, there is no maximum size to Xconq areas, but the minimum
  20.    size is set by mystical properties, and is not negotiable. */
  21.  
  22. #define MINWIDTH 3
  23. #define MINHEIGHT 3
  24.  
  25. /* An "area" is always basically a rectangular array of positions.  The
  26.    hex effect can be achieved by interpreting neighborliness and direction
  27.    differently, but that's all that's needed - storagewise the rect
  28.    array is still the best choice.  All of the "layers" are dynamically
  29.    allocated as needed, to save (considerable!) space. */
  30.  
  31. typedef struct a_area {
  32.     short width, height;    /* size of the area */
  33.     short halfheight;        /* height / 2 */
  34.     short maxdim;        /* max of the two dims */
  35.     short xwrap;        /* true if x coords wrap around */
  36.     int numcells;        /* number of cells in area */
  37.     int latitude, longitude;    /* position within whole world */
  38.     int cellwidth;        /* distance across one cell */
  39.     short fullwidth, fullheight; /* size of the area being used for data */
  40.     short fullx, fully;        /* offset within full area to get data from */
  41.     /* Pointers to the various "layers". */
  42.     struct a_unit **units;    /* pointer to units if any */
  43.     char *terrain;        /* terrain type at this spot */
  44.     char **auxterrain;        /* vector of extra types */
  45.     char *peopleside;        /* the overt side alignment of the locals */
  46.     short *features;        /* layer of ids of features */
  47.     short *elevations;        /* layer of elevations */
  48.     short constelev;        /* a constant elevation */
  49.     short avgelev;
  50.     short minelev;
  51.     short maxelev;
  52.     short **materials;        /* layer of materials in each cell */
  53.     short *temperature;        /* layer of cell temperatures */
  54.     short consttemp;        /* a constant temperature */
  55.     short *clouds;        /* types of clouds in the layer */
  56.     short *cloudbottoms;    /* altitudes of clouds of cloud layer */
  57.     short *cloudheights;    /* heights of clouds in the cloud layer */
  58.     short *winds;        /* layer of force/dir of winds. */
  59.     short constwinds;        /* constant wind */
  60.     /* These layers are for temporary use in calculations. */
  61.     short *tmp1;
  62.     short *tmp2;
  63.     short *tmp3;
  64. } Area;
  65.  
  66. /* Named geographical features. */
  67.  
  68. typedef struct a_feature {
  69.     int type;            /* index of the general type */
  70.     short id;            /* which one this is */
  71.     char *name;            /* the name of the region */
  72.     char *typename;        /* its category, such as "island" or "bay" */
  73.     /* something for syntax? "foo bay" vs "bay of foo" */
  74.     struct a_feature *next;    /* arranged in a linked list */
  75.     /* caches */
  76.     int size;
  77.     int x, y;
  78.     int mindiam;
  79.     int maxdiam;
  80.     int relabel;
  81. } Feature;
  82.  
  83. /* Paths. */
  84.  
  85. typedef struct a_waypoint {
  86.     int x, y;
  87.     int note;
  88. } Waypoint;
  89.  
  90. typedef struct a_path {
  91.     int numwps;
  92.     Waypoint wps[100];
  93. } Path;
  94.  
  95. /* Use this macro to get a area-spanning layer of the given type. */
  96.  
  97. #define malloc_area_layer(TYPE)  \
  98.   ((TYPE *) xmalloc(area.width * area.height * sizeof(TYPE)))
  99.  
  100. #define zero_area_layer(ADDR, TYPE)  \
  101.   (memset(ADDR, 0, area.width * area.height * sizeof(TYPE)))
  102.  
  103. /* General 2D malloced area array usage.  Names from Lisp. */
  104.  
  105. #define aref(m,x,y) ((m)[area.width * (y) + (x)])
  106.  
  107. #define aset(m,x,y,v) ((m)[area.width * (y) + (x)] = (v))
  108.  
  109. #define aadd(m,x,y,v) ((m)[area.width * (y) + (x)] += (v))
  110.  
  111. /* The unit is a raw pointer - this macro is used a *lot*.  This could
  112.    be space-optimized by using a 16-bit unit id. */
  113.  
  114. #define unit_at(x,y) aref(area.units, x, y)
  115.  
  116. #define set_unit_at(x,y,u) aset(area.units, x, y, u)
  117.  
  118. /* Iterate through all units in this cell (but not their occs) . */
  119.  
  120. #define for_all_stack(x,y,var)  \
  121.   for ((var) = unit_at((x), (y)); (var) != NULL;  (var) = (var)->nexthere)
  122.  
  123. /* Test if the terrain has been allocated yet. */
  124.  
  125. #define terrain_defined() (area.terrain != NULL)
  126.  
  127. /* The terrain at each cell is just the number of the terrain type. */
  128.  
  129. #define terrain_at(x,y) aref(area.terrain, x, y)
  130.  
  131. #define set_terrain_at(x,y,t) aset(area.terrain, x, y, t)
  132.  
  133. /* Auxiliary terrain array of layers. */
  134.  
  135. #define any_aux_terrain_defined() (area.auxterrain != NULL)
  136.  
  137. #define aux_terrain_defined(t)  \
  138.   (any_aux_terrain_defined() && area.auxterrain[t] != NULL)
  139.  
  140. #define aux_terrain_at(x,y,t) aref(area.auxterrain[t], x, y)
  141.  
  142. #define set_aux_terrain_at(x,y,t,v) aset(area.auxterrain[t], x, y, v)
  143.  
  144. /* Not really correct, should finish. */
  145.  
  146. #define any_borders_at(x,y,b) (aux_terrain_at(x, y, b) != 0)
  147.  
  148. #define border_at(x,y,dir,t)  \
  149.   (aux_terrain_defined(t) ? (aux_terrain_at(x, y, t) & (1 << (dir))) : FALSE)
  150.  
  151. #define any_connections_at(x,y,c) (aux_terrain_at(x, y, c) != 0)
  152.  
  153. #define connection_at(x,y,dir,t)  \
  154.   (aux_terrain_defined(t) ? (aux_terrain_at(x, y, t) & (1 << (dir))) : FALSE)
  155.  
  156. /* Elevation layer. */
  157.  
  158. #define world_is_flat() (minelev == maxelev)
  159.  
  160. #define elevations_defined() (area.elevations != NULL)
  161.  
  162. #define elev_at(x,y) aref(area.elevations, x, y)
  163.  
  164. #define set_elev_at(x,y,v) aset(area.elevations, x, y, v)
  165.  
  166. /* Feature layer. */
  167.  
  168. #define features_defined() (area.features != NULL)
  169.  
  170. /* The "raw feature" is its "short" identifier. */
  171.  
  172. #define raw_feature_at(x,y) aref(area.features, x, y)
  173.  
  174. #define set_raw_feature_at(x,y,f) aset(area.features, x, y, f)
  175.  
  176. /* Population layer. */
  177.  
  178. #define people_sides_defined() (area.peopleside != NULL)
  179.  
  180. #define people_side_at(x,y) aref(area.peopleside, x, y)
  181.  
  182. #define set_people_side_at(x,y,s) aset(area.peopleside, x, y, s)
  183.  
  184. /* A cell might be entirely uninhabited, so need an extra value to indicate. */
  185.  
  186. /* This value is chosen to be well above any possible MAXSIDES, and encodes
  187.    in layers as 'X', which is convenient. */
  188.  
  189. #define NOBODY (60)
  190.  
  191. #define populated(x,y) (people_side_at(x,y) != NOBODY)
  192.  
  193. /* Array of material layers. */
  194.  
  195. #define any_cell_materials_defined() (area.materials != NULL)
  196.  
  197. #define cell_material_defined(m) (area.materials[m] != NULL)
  198.  
  199. #define material_at(x,y,m) aref(area.materials[m], x, y)
  200.  
  201. #define set_material_at(x,y,m,v) aset(area.materials[m], x, y, v)
  202.  
  203. /* Temperature layer. */
  204.  
  205. #define temperatures_defined() (area.temperature != NULL)
  206.  
  207. #define temperature_at(x,y) aref(area.temperature, x, y)
  208.  
  209. #define set_temperature_at(x,y,v) aset(area.temperature, x, y, v)
  210.  
  211. /* Clouds layer. */
  212.  
  213. #define clouds_defined() (area.clouds != NULL)
  214.  
  215. #define raw_cloud_at(x,y) aref(area.clouds, x, y)
  216.  
  217. #define set_raw_cloud_at(x,y,v) aset(area.clouds, x, y, v)
  218.  
  219. #define cloud_bottoms_defined() (area.cloudbottoms != NULL)
  220.  
  221. #define raw_cloud_bottom_at(x,y) aref(area.cloudbottoms, x, y)
  222.  
  223. #define set_raw_cloud_bottom_at(x,y,v) aset(area.cloudbottoms, x, y, v)
  224.  
  225. #define cloud_heights_defined() (area.cloudheights != NULL)
  226.  
  227. #define raw_cloud_height_at(x,y) aref(area.cloudheights, x, y)
  228.  
  229. #define set_raw_cloud_height_at(x,y,v) aset(area.cloudheights, x, y, v)
  230.  
  231. /* Winds layer. */
  232.  
  233. #define winds_defined() (area.winds != NULL)
  234.  
  235. #define raw_wind_at(x,y) aref(area.winds, x, y)
  236.  
  237. #define set_raw_wind_at(x,y,v) aset(area.winds, x, y, v)
  238.  
  239. #define wind_dir_at(x,y) (raw_wind_at(x, y) & 0x07)
  240.  
  241. #define wind_force_at(x,y) (raw_wind_at(x, y) >> 3)
  242.  
  243. #define set_wind_at(x,y,d,f) (set_raw_wind_at(x, y, ((f) << 3) | (d)))
  244.  
  245. #define CALM (0)
  246.  
  247. /* Handlers for scratch layers. */
  248.  
  249. #define tmp1_at(x,y) aref(area.tmp1, x, y)
  250.  
  251. #define set_tmp1_at(x,y,v) aset(area.tmp1, x, y, v)
  252.  
  253. #define tmp2_at(x,y) aref(area.tmp2, x, y)
  254.  
  255. #define set_tmp2_at(x,y,v) aset(area.tmp2, x, y, v)
  256.  
  257. #define tmp3_at(x,y) aref(area.tmp3, x, y)
  258.  
  259. #define set_tmp3_at(x,y,v) aset(area.tmp3, x, y, v)
  260.  
  261. /* This little macro implements wraparound in the x direction. */
  262. /* The stupid add of shifted width is for the benefit of brain-damaged
  263.    mod operators that don't handle negative numbers properly. */
  264.  
  265. #define wrapx(x) (area.xwrap ? (((x) + (area.width << 8)) % area.width) : (x))
  266.  
  267. /* Constrain y to northern and southern edges. */
  268.  
  269. #define limit(y) (max(0, min((y), (area.height-1))))
  270.  
  271. #define interior(y) (max(1, min((y), (area.height-2))))
  272.  
  273. #define xy_in_dir(x,y,d,nx,ny) \
  274.   (nx) = wrapx((x) + dirx[d]);  (ny) = (y) + diry[dir];
  275.  
  276. /* in_area and inside_area are very heavily used; any optimization will likely
  277.    speed up Xconq overall. */
  278.  
  279. /* Test whether x,y is a valid position anywhere in the current area. */
  280.  
  281. #define in_area(x, y)  \
  282.   (between(0, (y), area.height-1) && x_in_area(x, y))
  283.  
  284. #define x_in_area(x, y)  \
  285.   (area.xwrap ? TRUE : (between(0, (x), area.width-1) &&  \
  286.             between(area.halfheight,  \
  287.                 (x)+(y),  \
  288.                 area.width+area.halfheight-1)))
  289.  
  290. /* This is true if the given x, y position is a valid position for units. */
  291.  
  292. /* Does x testing work right for even/odd heights? */
  293.  
  294. #define inside_area(x, y)  \
  295.   (between(1, (y), area.height-2) && x_inside_area(x, y))
  296.  
  297. #define x_inside_area(x, y)  \
  298.   (area.xwrap ? TRUE : (between(1, (x), area.width-2) &&  \
  299.             between(area.halfheight+1,  \
  300.                 (x)+(y),  \
  301.                 area.width+area.halfheight-2)))
  302.  
  303. /* Iteration over all valid cell positions in a area.  These should be
  304.    used carefully, since they don't (can't) have any grouping braces
  305.    embedded. */
  306.  
  307. #define for_all_cells(x,y)  \
  308.   for (x = 0; x < area.width; ++x)  \
  309.     for (y = 0; y < area.height; ++y)  \
  310.       if (x_in_area(x, y))
  311.  
  312. /* This doesn't generate positions along area edges.  Typically more
  313.    useful within game. */
  314.  
  315. #define for_all_interior_cells(x,y)  \
  316.   for (x = 0; x < area.width; ++x)  \
  317.     for (y = 1; y < area.height - 1; ++y)  \
  318.       if (x_inside_area(x, y))
  319.  
  320. /* Returns the lighting state of a given position. */
  321. /* (should opencode distance call here) */
  322.  
  323. #define lighting(x,y,snx,sny)  \
  324.   ((world_distance(x, y, snx, sny) < world.daylight_width) ? 2 : \
  325.    ((world_distance(x, y, snx, sny) < world.twilight_width) ? 1 : 0))
  326.  
  327. /* True if the given x,y is dark. */
  328.  
  329. #define night_at(x,y)  \
  330.   (daynight && lighting((x), (y), (int) sunx, (int) suny) == 0)
  331.  
  332. /* World-related variables. */
  333.  
  334. extern World world;
  335.  
  336. extern Area area;
  337.  
  338. extern int midturnrestore;
  339.  
  340. extern int numcelltypes;
  341. extern int numbordtypes;
  342. extern int numconntypes;
  343. extern int numcoattypes;
  344.  
  345. extern int minelev;
  346. extern int maxelev;
  347. extern int mintemp;
  348. extern int maxtemp;
  349. extern int minwindforce;
  350. extern int maxwindforce;
  351.  
  352. extern int any_materials_in_terrain;
  353. extern int any_temp_variation;
  354. extern int any_temp_variation_in_layer;
  355. extern int any_wind_variation;
  356. extern int any_wind_variation_in_layer;
  357. extern int any_clouds;
  358.  
  359. /* World-related functions. */
  360.  
  361. extern void init_world PARAMS ((void));
  362. extern int set_world_circumference PARAMS ((int circum, int warn));
  363. extern int set_area_shape PARAMS ((int width, int height, int warn));
  364. extern int valid_area_shape PARAMS ((int width, int height, int warn));
  365. extern void check_area_shape PARAMS ((void));
  366. extern void calculate_world_globals PARAMS ((void));
  367. extern void count_terrain_subtypes PARAMS ((void));
  368. extern void final_init_world PARAMS ((void));
  369.  
  370. extern void allocate_area_terrain PARAMS ((void));
  371. extern void allocate_area_aux_terrain PARAMS ((int t));
  372. extern void allocate_area_scratch PARAMS ((int n));
  373. extern void allocate_area_elevations PARAMS ((void));
  374. extern void allocate_area_temperatures PARAMS ((void));
  375. extern void allocate_area_people_sides PARAMS ((void));
  376. extern void allocate_area_material PARAMS ((int m));
  377. extern void allocate_area_clouds PARAMS ((void));
  378. extern void allocate_area_cloud_altitudes PARAMS ((void));
  379. extern void allocate_area_cloud_bottoms PARAMS ((void));
  380. extern void allocate_area_cloud_heights PARAMS ((void));
  381. extern void allocate_area_winds PARAMS ((void));
  382.  
  383. extern int fn_terrain_at PARAMS ((int x, int y));
  384. extern int fn_aux_terrain_at PARAMS ((int x, int y));
  385. extern int fn_feature_at PARAMS ((int x, int y));
  386. extern int fn_elevation_at PARAMS ((int x, int y));
  387. extern int fn_people_side_at PARAMS ((int x, int y));
  388. extern int fn_material_at PARAMS ((int x, int y));
  389. extern int fn_temperature_at PARAMS ((int x, int y));
  390. extern int fn_raw_cloud_at PARAMS ((int x, int y));
  391. extern int fn_raw_cloud_bottom_at PARAMS ((int x, int y));
  392. extern int fn_raw_cloud_height_at PARAMS ((int x, int y));
  393. extern int fn_raw_wind_at PARAMS ((int x, int y));
  394.  
  395. extern void fn_set_terrain_at PARAMS ((int x, int y, int val));
  396. extern void fn_set_aux_terrain_at PARAMS ((int x, int y, int val));
  397. extern void fn_set_people_side_at PARAMS ((int x, int y, int val));
  398. extern void fn_set_raw_feature_at PARAMS ((int x, int y, int val));
  399. extern void fn_set_elevation_at PARAMS ((int x, int y, int val));
  400. extern void fn_set_material_at PARAMS ((int x, int y, int val));
  401. extern void fn_set_temperature_at PARAMS ((int x, int y, int val));
  402. extern void fn_set_raw_wind_at PARAMS ((int x, int y, int val));
  403. extern void fn_set_raw_cloud_at PARAMS ((int x, int y, int val));
  404. extern void fn_set_raw_cloud_bottom_at PARAMS ((int x, int y, int val));
  405. extern void fn_set_raw_cloud_height_at PARAMS ((int x, int y, int val));
  406.  
  407. extern void change_terrain_type PARAMS ((int x, int y, int t2));
  408.  
  409. extern int search_around PARAMS ((int x0, int y0, int maxdist,
  410.                  int (*pred)(int, int),
  411.                  int *rxp, int *ryp, int incr));
  412. extern int search_and_apply PARAMS ((int x0, int y0, int maxdist,
  413.                     int (*pred)(int, int),
  414.                     int *rxp, int *ryp, int incr,
  415.                     void (*fn)(int, int), int num));
  416. extern void apply_to_area PARAMS ((int x0, int y0, int dist,
  417.                   void (*fn)(int, int)));
  418. extern void apply_to_area_plus_edge PARAMS ((int x0, int y0, int dist,
  419.                         void (*fn)(int, int)));
  420. extern void apply_to_ring PARAMS ((int x0, int y0, int distmin, int distmax,
  421.                   void (*fn)(int, int)));
  422. extern void apply_to_hexagon PARAMS ((int x0, int y0, int w2, int h2,
  423.                      void (*fn)(int, int)));
  424. extern void apply_to_path PARAMS ((int fx, int fy, int tx, int ty,
  425.                   int (*dirtest)(int x, int y, int dir),
  426.                   int (*dirsort)(int x, int y, int *dirchoices, int numchoices),
  427.                   int (*fn)(int x, int y, int dir, int j, int numchoices),
  428.                   int shortest));
  429. #if 0
  430. int find_path PARAMS ((int fx, int fy, int tx, int ty,
  431.               int (*chooser)(int, int, int, int, int *),
  432.               int maxwps, Waypoint *waypoints, int *numwpsp));
  433. #endif
  434.  
  435. #if 0
  436. extern int border_at PARAMS ((int x, int y, int dir, int t));
  437. #endif
  438. extern void set_border_at PARAMS ((int x, int y, int dir, int t, int onoff));
  439. #if 0
  440. extern int connection_at PARAMS ((int x, int y, int dir, int t));
  441. #endif
  442. extern void set_connection_at PARAMS ((int x, int y, int dir, int t, int onoff));
  443. extern void patch_linear_terrain PARAMS ((int t));
  444. extern void init_features PARAMS ((void));
  445. extern Feature *create_feature PARAMS ((char *typename, char *name));
  446. extern Feature *find_feature PARAMS ((int fid));
  447. extern Feature *feature_at PARAMS ((int x, int y));
  448. extern void set_feature_type_name PARAMS ((Feature *feature, char *typename));
  449. extern void set_feature_name PARAMS ((Feature *feature, char *name));
  450. extern void destroy_feature PARAMS ((Feature *feature));
  451. extern void renumber_features PARAMS ((void));
  452. extern void compute_all_feature_centroids PARAMS ((void));
  453. extern void compute_feature_centroid PARAMS ((Feature *feature));
  454.  
  455. extern int point_in_dir PARAMS ((int x, int y, int dir, int *xp, int *yp));
  456. extern int interior_point_in_dir PARAMS ((int x, int y, int dir,
  457.                       int *xp, int *yp));
  458. extern int point_in_dir_n PARAMS ((int x, int y, int dir, int n,
  459.                    int *xp, int *yp));
  460. extern int interior_point_in_dir_n PARAMS ((int x, int y, int dir, int n,
  461.                         int *xp, int *yp));
  462. extern int random_point PARAMS ((int *xp, int *yp));
  463. extern int random_edge_point PARAMS ((int *xp, int *yp));
  464. extern int random_point_near PARAMS ((int cx, int cy, int radius,
  465.                       int *xp, int *yp));
  466. extern int random_point_in_area PARAMS ((int cx, int cy, int rx, int ry,
  467.                      int *xp, int *yp));
  468. extern void terrain_subtype_warning PARAMS ((char *context, int t));
  469. extern int approx_dir PARAMS ((int dx, int dy));
  470. extern int hextant PARAMS ((int dx, int dy));
  471. extern int distance PARAMS ((int x1, int y1, int x2, int y2));
  472. extern int world_distance PARAMS ((int x1, int y1, int x2, int y2));
  473. extern int closest_dir PARAMS ((int x, int y));
  474.  
  475. #ifdef DESIGNERS
  476.  
  477. extern void paint_cell PARAMS ((Side *side, int x, int y, int r, int t));
  478. extern void paint_border PARAMS ((Side *side, int x, int y, int dir,
  479.                   int t, int mode));
  480. extern void paint_connection PARAMS ((Side *side, int x, int y, int dir,
  481.                       int t, int mode));
  482. extern void paint_coating PARAMS ((Side *side, int x, int y, int r,
  483.                    int t, int depth));
  484. extern void paint_people PARAMS ((Side *side, int x, int y, int r, int s));
  485. extern void paint_feature PARAMS ((Side *side, int x, int y, int r, int f));
  486. extern void paint_elevation PARAMS ((Side *side, int x, int y, int r,
  487.                      int elev));
  488. extern void paint_temperature PARAMS ((Side *side, int x, int y, int r,
  489.                        int temp));
  490. extern void paint_material PARAMS ((Side *side, int x, int y, int r,
  491.                     int m, int amt));
  492. extern void paint_clouds PARAMS ((Side *side, int x, int y, int r,
  493.                   int cloudtype, int bot, int hgt));
  494. extern void paint_winds PARAMS ((Side *side, int x, int y, int r,
  495.                  int dir, int force));
  496.  
  497. #endif /* DESIGNERS */
  498.